<%-- 
    Document   : post-add-action
    Created on : Feb 13, 2012, 11:19:40 PM
    Author     : PhucAn
--%>
<%@page import="util.SharingUtil"%>
<%@page import="dao.PostDAO"%>
<%@page import="java.util.Date"%>
<%@page import="dto.Post"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.QuestionCategoryDAO"%>
<%@page import="dto.QuestionCategory"%>
<%@page import="dto.PostCategory"%>
<%
    QuestionCategoryDAO daoCate = new QuestionCategoryDAO();
    ArrayList<QuestionCategory> list = daoCate.getAllCategories();
    request.setAttribute("cateList", list);

    if (request.getParameter("add") != null) {
        PostDAO daoPost = new PostDAO();
        Post post = new Post();
        post.setTitle(request.getParameter("title"));
        post.setContent(request.getParameter("content"));
        post.setState(request.getParameter("is-public")!=null);
        post.getCategory().setId(Integer.parseInt(request.getParameter("category")));
        post.getCls().setId(SharingUtil.getCurrentClass(session).getId());
        post.setModifiedDate(new Date());
        
        post.setAuthor(SharingUtil.getUser(session));        
        if (daoPost.create(post) > 0) {
            SharingUtil.showMessage(session, "New post is created");
        } else {
            SharingUtil.showMessage(session, "Error. Current post could not be saved");
        }
    }
%>